package com.yuandian.utils.poi.converter;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.enums.CellDataTypeEnum;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.CellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.yuandian.annotation.AddSuffix;

/**
 * 说明：添加后缀转换器
 * @Author yanjun
 * @Date 2020/12/28 10:57
 */
public class AddSuffixConverter implements Converter<Object> {

    /**
     * java类型
     * @return
     */
    @Override
    public Class supportJavaTypeKey() {
        return Object.class;
    }

    /**
     * excel类型
     * @return
     */
    @Override
    public CellDataTypeEnum supportExcelTypeKey() {
        return CellDataTypeEnum.STRING;
    }



    /**
	 * 写时调用
	 *
	 * @param value
	 * @param contentProperty
	 * @param globalConfiguration
	 * @return
	 * @throws Exception
	 */
    @Override
    public WriteCellData<?> convertToExcelData(Object value, ExcelContentProperty contentProperty,
											   GlobalConfiguration globalConfiguration) throws Exception {
        AddSuffix addSuffixAnno = contentProperty.getField().getAnnotation(AddSuffix.class);
        if (addSuffixAnno != null) {
            return new WriteCellData(value + addSuffixAnno.value());
        }
        return new WriteCellData((String) value);
    }
}
